﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class EditRepairProblem : BasePage
    {
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[ID] != null && Request.QueryString[ID] != "")
                id = int.Parse(Request.QueryString[ID]);
            else
            {
                CloseDlg(0);
                return;
            }

            if (!IsPostBack)
            {
                RepairJobProblems prob = new RepairJobProblems();
                prob.LoadByPrimaryKey(id);
                if (prob.RowCount <= 0)
                {
                    CloseDlg(0);
                    return;
                }

                txtDesc.Text = prob.s_Description;
                txtPrice.Text = prob.s_Price;
            }
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();

            try
            {
                tx.BeginTransaction();

                RepairJobProblems prob = new RepairJobProblems();
                prob.LoadByPrimaryKey(id);
                if (prob.RowCount <= 0)
                {
                    throw new Exception("(*) Item not available!");
                }

                prob.s_Price = txtPrice.Text;
                prob.s_Description = txtDesc.Text;
                prob.Save();

                RepairJob job = new RepairJob();
                job.LoadByPrimaryKey(prob.RepairJobID);
                job.StatusID = RepairStatus.NewQuote;
                job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                job.LastModifiedByUser = SessionUser.ID;
                job.Save();

                //insert status history
                RepairStatusHistories hist = new RepairStatusHistories();
                hist.AddNew();
                hist.s_RepairJobID = job.s_ID;
                hist.s_RepairStatusID = job.s_StatusID;
                hist.UpdatedDate = job.LastModifiedDate;
                hist.UpdatedByUser = SessionUser.ID;
                hist.BranchID = Branches.CurrentBranchID;
                hist.Save();

                tx.CommitTransaction();
                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("", "", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
                return false;
            }

        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                CloseDlg(1);
        }
    }
}